package com.designpatterns.singletonpattern;

/**
 * 线程安全的懒汉式单例
 */
public class SingletonLazyThreadSafe1 {
    /**
     * private禁止外界随意访问 static使用类变量缓存实例
     */
    private static SingletonLazyThreadSafe1 SingletonLazyThreadSafe;

    /**
     * 私有化构造方法禁止外界随意创建本类实例
     */
    private SingletonLazyThreadSafe1() {
    }

    /**
     * 对外暴露公共方法供外界获取该单例
     */
    public static synchronized SingletonLazyThreadSafe1 getSingletonLazyThreadSafe() {
        if (SingletonLazyThreadSafe == null) {
            Thread.yield();
            SingletonLazyThreadSafe = new SingletonLazyThreadSafe1();
        }
        return SingletonLazyThreadSafe;
    }
}